/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#define _GD25Q_HAL_MODEL_
#include "sys.h"
#include "gd25q_hal.h"
#include "gd25q_cmd.h"

#define GDSPI SPI2
#define ISR_IRQn SPI2_IRQn

#define PDMA_CH_SPI_RX 11
#define PDMA_CH_SPI_TX 10

uint32_t DataFixed;

void gdmaRxIsr(void);
void gdmaTxIsr(void);

void FlashQ25ReadDmaCallBack(void);

/*****************************************************************************/ /*!
* @brief   hal init.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gd25_spi_init()
{
    spi_parameter_struct spi_init_struct;

    //cs
    INTX_DISABLE();

    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
    gd25_cs(1);
    rcu_periph_clock_enable(RCU_SPI2);
    rcu_periph_reset_enable(RCU_SPI2RST);
    rcu_periph_reset_disable(RCU_SPI2RST);

    /* SPI0_SCK(PB3), SPI0_MISO(PB4) and SPI0_MOSI(PB5) GPIO pin configuration */
    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5);
    gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4);

    INTX_ENABLE();

    /* SPI0 parameter config */
    spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
    spi_init_struct.device_mode = SPI_MASTER;
    ;
    spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
    ;
    spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
    spi_init_struct.nss = SPI_NSS_SOFT;
    spi_init_struct.prescale = SPI_PSC_8;
    spi_init_struct.endian = SPI_ENDIAN_MSB;
    ;
    spi_init(SPI0, &spi_init_struct);

    spi_enable(SPI0);

    gd25_cs(0);
    gd25_spi_rw(0xFF);
    gd25_cs(1);

    b_gd25_spi_dma_busy = 0;
}

/*****************************************************************************/ /*!
* @brief   spi data read and write.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
uint8_t gd25_spi_rw(uint8_t dataW)
{

    volatile uint8_t dataR;

    while (SPI_STAT(SPI0) & SPI_STAT_RBNE)
        dataR = SPI_DATA(SPI0);

    SPI_DATA(SPI0) = (uint32_t)dataW;
    NOP8();

    while (!(SPI_STAT(SPI0) & SPI_STAT_RBNE))
        ;

    dataR = SPI_DATA(SPI0);

    return (dataR);
}

uint8_t gd25_spi_read(uint8_t *datar, uint32_t Len)
{

    volatile register uint8_t dataR;

    while (SPI_STAT(SPI0) & SPI_STAT_RBNE)
        dataR = SPI_DATA(SPI0);

    while (Len--)
    {
        SPI_DATA(SPI0) = 0xFF;
        NOP8();
        while (!(SPI_STAT(SPI0) & SPI_STAT_RBNE))
            ;
        *datar++ = SPI_DATA(SPI0);
    }
    return (dataR);
}

uint8_t gd25_spi_write(uint8_t *dataw, uint32_t Len)
{
    volatile register uint8_t dataR;

    while (SPI_STAT(SPI0) & SPI_STAT_RBNE)
        dataR = SPI_DATA(SPI0);

    while (Len--)
    {
        SPI_DATA(SPI0) = *dataw++;
        NOP8();
        while (!(SPI_STAT(SPI0) & SPI_STAT_RBNE))
            ;
        dataR = SPI_DATA(SPI0);
    }

    return (dataR);
}

/*****************************************************************************/ /*!
* @brief   DMA control.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gd25_spi_dma_read(uint8_t *Data, uint32_t Len)
{
    //b_gd25_spi_dma_busy =1;
}

/*****************************************************************************/ /*!
* @brief   DMA control.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gd25_spi_dma_write(uint8_t *Data, uint32_t Len)
{
    //b_gd25_spi_dma_busy =1;
}

/*****************************************************************************/ /*!
* @brief   RX DMA Isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gdmaRxIsr()
{

    b_gd25_spi_dma_busy = 0;
}

/*****************************************************************************/ /*!
* @brief   TX DMA Isr.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void gdmaTxIsr()
{
}
